package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// CORSMiddleware 解决跨域问题
func CORSMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		context.Writer.Header().Set("Access-Control-Allow-Origin","http://localhost:8080")//实用*表示所有
		context.Writer.Header().Set("Access-Control-Max-Age","86400")//最长时间
		context.Writer.Header().Set("Access-Control-Allow-Methods","*")//允许访问的方法
		context.Writer.Header().Set("Access-Control-Allow-Headers","*")//允许的请求头
		context.Writer.Header().Set("Access-Control-Allow-Credentials","true")
		if context.Request.Method == http.MethodOptions{
			context.AbortWithStatus(200)
		}else{
			context.Next()
		}

	}
}
